package com.newbingo.quartz;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.springframework.aop.AfterReturningAdvice;
import com.newbingo.dto.MemberDto;
import com.newbingo.entity.Agent;
import com.newbingo.entity.BettingItem;
import com.newbingo.entity.GameRebate;
import com.newbingo.entity.GameRebateLog;
import com.newbingo.entity.Member;
import com.newbingo.service.BettingItemService;
import com.newbingo.service.GameRebateLogService;
import com.newbingo.service.imple.IAgentService;
import com.newbingo.service.imple.IBettingItemService;
import com.newbingo.service.imple.IGameRebateLogService;
import com.newbingo.service.imple.IGameRebateService;
import com.newbingo.service.imple.IMemberService;

/*
 * 游戏返利
 * spring定时执行,在时间段内的投注的返利
 * 配置任务调度为每天23：55执行
 */
public class GameRebateQuartz {
	private IAgentService agentService;
	private IMemberService memberService;
	private IGameRebateService gameRebateService;
	private IBettingItemService bettingItemSerivce;
	private IGameRebateLogService gameRebateLogService;

	public void doRebate() {
		// 查询出代理商，迭代获取每个代理商的最新返利设置
		List<Agent> agents = agentService.findAll();
		for (Agent agent : agents) {
			// 获取返利设置，比较当前时间和返利时间，返利截止时间的日期和当前系统时间在同一天，则进行返利，执行账户操作
			GameRebate rebate = gameRebateService.findLastestRebateByAgent(agent.getUno());
			if (rebate != null) {
				Date now = new Date();
				// 表示返利已经到截止时间前
				if (rebate.getEndTime().getTime() - now.getTime() <= 24 * 60 * 60 * 1000) {
					Set<Member> members = agent.getMembers();
					for (Member member : members) {
						// 获取返利对象在返利时间内的会员投注的集合
						List<BettingItem> items = bettingItemSerivce
								.findBettingItemsByPeriodTime(rebate.getStartTime(), rebate.getEndTime(),member.getUno(),rebate.getGame().getGameno());
						// 定义totalAmount投注总额变量，迭代items集合获取投注总额
						int totalAmount = 0;
						for (BettingItem item : items) {
							totalAmount += item.getAmount();
						}
						//满足返利投注金额条件，则进行持久化操作
						if(totalAmount>=rebate.getAmount()){
							//计算返利的金额
							int rebateAmount=(int)(rebate.getAmount()*totalAmount/1000);
							//保存返利记录
							GameRebateLog rebateLog=new GameRebateLog();
							rebateLog.setMember(member);
							rebateLog.setRebateDate(now);
							rebateLog.setAmount(rebateAmount);
							gameRebateLogService.save(rebateLog);
							//计算余额，并持久化
							member.getAccount().setAmount(member.getAccount().getAmount()+rebateAmount);
							memberService.updateMember(member);
						}
					}
				}
			}
		}
	}

	public IMemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(IMemberService memberService) {
		this.memberService = memberService;
	}

	public IGameRebateService getGameRebateService() {
		return gameRebateService;
	}

	public void setGameRebateService(IGameRebateService gameRebateService) {
		this.gameRebateService = gameRebateService;
	}

	public IBettingItemService getBettingItemSerivce() {
		return bettingItemSerivce;
	}

	public void setBettingItemSerivce(IBettingItemService bettingItemSerivce) {
		this.bettingItemSerivce = bettingItemSerivce;
	}

	public IGameRebateLogService getGameRebateLogService() {
		return gameRebateLogService;
	}

	public void setGameRebateLogService(
			IGameRebateLogService gameRebateLogService) {
		this.gameRebateLogService = gameRebateLogService;
	}

	public IAgentService getAgentService() {
		return agentService;
	}

	public void setAgentService(IAgentService agentService) {
		this.agentService = agentService;
	}
}
